package com.roobtyan.todomemback.service.serviceImpl;

import com.roobtyan.todomemback.entity.CardDir1;
import com.roobtyan.todomemback.entity.MemCardExample;
import com.roobtyan.todomemback.mapper.CardDir1Mapper;
import com.roobtyan.todomemback.mapper.MemCardMapper;
import com.roobtyan.todomemback.service.CardDirService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CardDirServiceImpl implements CardDirService {
    @Resource
    private CardDir1Mapper cardDir1Mapper;
    @Resource
    private MemCardMapper cardMapper;

    @Override
    @Transactional
    public int insertDir1(CardDir1 cardDir1) {
        return cardDir1Mapper.insertSelective(cardDir1);
    }

    @Override
    @Transactional
    public void deleteDir1(int dir1Id) {
        // 同样会删除所有dir1的card
        cardDir1Mapper.deleteByPrimaryKey(dir1Id);
        MemCardExample memCardExample = new MemCardExample();
        MemCardExample.Criteria criteria = memCardExample.createCriteria();
        criteria.andDir1IdEqualTo(dir1Id);
        cardMapper.deleteByExample(memCardExample);
    }

    @Override
    @Transactional
    public void updateDir1(CardDir1 cardDir1) {
        cardDir1Mapper.updateByPrimaryKeySelective(cardDir1);
    }

    @Override
    public List<CardDir1> getAllDir1() {
        return cardDir1Mapper.selectByExample(null);
    }
}
